package com.tianyang.yukuaifu.business.login.view;

import android.content.Intent;
import android.net.http.SslError;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：隐私政策查看浏览器
 */
public class PrivacyBrowserActivity extends BaseTitleActivity {

    @BindView(R.id.progressBar)
    ProgressBar vProgressBar;
    @BindView(R.id.titleTv)
    TextView vTitleTv;
    @BindView(R.id.webview)
    WebView mWebView;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_privacy_web;
    }

    @Override
    protected void initView() {
        super.initView();
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                vProgressBar.setVisibility(View.GONE);
                if (TextUtils.isEmpty(vTitleTv.getText()) && mWebView.getTitle() != null) {
                    vTitleTv.setText(mWebView.getTitle());
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                return super.shouldOverrideUrlLoading(webView,url);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        String title = getIntent().getStringExtra(MainConfig.INTENT_TITLE);
        if (!TextUtils.isEmpty(title)) {
            vTitleTv.setText(title);
        }
        String url = getIntent().getStringExtra(MainConfig.INTENT_WEB_URL);
        if (!TextUtils.isEmpty(url)) {
            mWebView.loadUrl(url);
        }
    }

    @OnClick({R.id.leftIv, R.id.closeIv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftIv:
                onBackPressed();
                break;
            case R.id.closeIv:
                finish();
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent == null || mWebView == null || intent.getData() == null)
            return;
        mWebView.loadUrl(intent.getData().toString());
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null)
            mWebView.destroy();
    }
}
